/**  
 * @Title: CmpSyncOfflineService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
 */  
package com.fsxgt.datagrid.component.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fsxgt.datagrid.component.bo.CmpSyncOfflineBO;
import com.fsxgt.datagrid.component.bo.CmpSyncOfflineColumnBO;
import com.fsxgt.datagrid.component.dao.CmpSyncOfflineColumnMapper;
import com.fsxgt.datagrid.component.dao.CmpSyncOfflineMapper;
import com.fsxgt.datagrid.datax.tool.DataxSecretUtil;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;
import com.fsxgt.datagrid.flow.service.IComponentService;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: CmpSyncOfflineService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
*/
@Slf4j
@Service
public class CmpSyncOfflineService implements IComponentService{
	
	@Autowired
	private FlowInfoMapper flowInfoMapper;
	
	@Autowired
	private CmpSyncOfflineMapper cmpSyncOfflineMapper;
	@Autowired
	private CmpSyncOfflineColumnMapper cmpSyncOfflineColumnMapper;
	
	@Transactional(rollbackFor = Exception.class)
	public void saveCmpSyncOffline(CmpSyncOfflineBO cmpSyncOfflineBO) {
		CmpSyncOfflineBO bo = cmpSyncOfflineMapper.get(cmpSyncOfflineBO.getFlowInfoId(),cmpSyncOfflineBO.getComponentSeq());

		if(cmpSyncOfflineBO.getJsonText()!=null&&cmpSyncOfflineBO.getJsonText().trim().length()>0) {
			cmpSyncOfflineBO.setJsonText(DataxSecretUtil.dataxEnCrypt(cmpSyncOfflineBO.getJsonText()));
		}
		
		if(bo==null) {
			cmpSyncOfflineMapper.insert(cmpSyncOfflineBO);
		}else {
			cmpSyncOfflineMapper.update(cmpSyncOfflineBO);
		}
		
		for(CmpSyncOfflineColumnBO column:cmpSyncOfflineBO.getColumnList()) {
			column.setComponentSeq(cmpSyncOfflineBO.getComponentSeq());
			column.setFlowInfoId(cmpSyncOfflineBO.getFlowInfoId());
		}
		cmpSyncOfflineColumnMapper.delete(cmpSyncOfflineBO.getFlowInfoId(),cmpSyncOfflineBO.getComponentSeq());

		for(int i=0;i<cmpSyncOfflineBO.getColumnList().size();i++) {
			cmpSyncOfflineBO.getColumnList().get(i).setSeq(i+1);
		}

		
		if(cmpSyncOfflineBO.getColumnList().size()>0) {
			cmpSyncOfflineColumnMapper.insert(cmpSyncOfflineBO.getColumnList());

		}
		
		flowInfoMapper.updateFlowComponentName(cmpSyncOfflineBO.getName(), cmpSyncOfflineBO.getFlowInfoId(), cmpSyncOfflineBO.getComponentSeq());

		
	}
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(CmpSyncOfflineBO cmpSyncOfflineBO) {
		deleteComponent(cmpSyncOfflineBO.getFlowInfoId(),cmpSyncOfflineBO.getComponentSeq());
	}
	/**  
	 * @Title: deleteComponent
	 * @Description: TODO(描述)
	 * @param flowInfoId
	 * @param seq 
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#deleteComponent(java.lang.String, java.lang.String) 
	 * @author liuyi
	 * @date 2023-05-08 09:08:12 
	 */ 
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(String flowInfoId, String seq) {
		// TODO Auto-generated method stub
		cmpSyncOfflineMapper.delete(flowInfoId,seq);
		cmpSyncOfflineColumnMapper.delete(flowInfoId,seq);
	}
	/**  
	 * @Title: getComponentType
	 * @Description: TODO(描述)
	 * @return 
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#getComponentType() 
	 * @author liuyi
	 * @date 2023-05-08 09:08:12 
	 */ 
	@Override
	public String getComponentType() {
		// TODO Auto-generated method stub
		return "Sync Offline";
	}
	/**  
	 * @Title: getSyncOffline
	 * @Description: TODO(描述)
	 * @param bo
	 * @author liuyi
	 * @date 2023-05-08 09:51:14 
	 */  
	public CmpSyncOfflineBO getSyncOffline(CmpSyncOfflineBO cmpSyncOfflineBO) {
		// TODO Auto-generated method stub
		CmpSyncOfflineBO bo = cmpSyncOfflineMapper.get(cmpSyncOfflineBO.getFlowInfoId(),cmpSyncOfflineBO.getComponentSeq());
		if(bo!=null) {
			List<CmpSyncOfflineColumnBO> list = cmpSyncOfflineColumnMapper.getList(cmpSyncOfflineBO.getFlowInfoId(),cmpSyncOfflineBO.getComponentSeq());
			bo.setColumnList(list);
		}
		return bo;
	}
	/**  
	 * @Title: getConfig
	 * @Description: TODO(描述)
	 * @param flowId
	 * @param seq
	 * @return 
	 * @see com.fsxgt.datagrid.core.service.IComponentService#getConfig(java.lang.String, java.lang.String) 
	 * @author liuyi
	 * @date 2023-05-25 15:24:47 
	 */ 
	@Override
	public Object getConfig(String flowId, String seq) {
		// TODO Auto-generated method stub
		
		CmpSyncOfflineBO bo = new CmpSyncOfflineBO();
		bo.setFlowInfoId(flowId);
		bo.setComponentSeq(seq);
		
		return getSyncOffline(bo);
	}
}
